﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Scene.Physics;
using EngineOfEvermore.Common;
using System.Drawing;
using System.Linq;
using System.ComponentModel;

namespace EngineOfEvermore.Testing
{
    [Browsable( false )]
	[PerInstance]
    public class SensorTestComponent : SceneObjectComponent
    {
        class _UpdateTask : Task
        {
            SensorTestComponent _owner;

            public _UpdateTask( SensorTestComponent owner ) :
                base( 10000 )
            {
                _owner = owner;
            }

            public override void Execute( LogicUpdateEventArgs e )
            {
                var oldObjectList = _owner._sceneObjectsInSensor;
                var currentObjectList = _owner._sensorComponent.Sensors["left"].Overlaps.Union(
                    _owner._sensorComponent.Sensors["right"].Overlaps );

                var newObjects = currentObjectList.Except( oldObjectList );
                var removedObjects = oldObjectList.Except( currentObjectList );

                foreach ( SceneObject sco in newObjects )
                {
                    sco.AddComponent( new HighlightTestComponent() );
                }

                foreach ( SceneObject sco in removedObjects )
                {
                    sco.RemoveComponent( sco.GetComponent<HighlightTestComponent>() );
                }

                _owner._sceneObjectsInSensor.Clear();
                _owner._sceneObjectsInSensor.AddRange( currentObjectList );
            }
        }

        SensorComponent _sensorComponent;
        List<SceneObject> _sceneObjectsInSensor = new List<SceneObject>();
        _UpdateTask _updateTask;

        public SensorTestComponent()
        {
            _updateTask = new _UpdateTask( this );
        }

        protected override void OnAddToSceneObject( SceneObject sceneObject )
        {
            base.OnAddToSceneObject( sceneObject );

            _sensorComponent = new SensorComponent();

            CollisionShape leftShape = new CircleShape( 10, new Vector2d( -50, 0 ) );
            leftShape.CollisionGroup = "CharacterSensor";
            Sensor leftSensor = new Sensor( leftShape );
            leftSensor.Name = "left";
            _sensorComponent.Sensors.Add( leftSensor );

            CollisionShape rightShape = new CircleShape( 10, new Vector2d( 50, 0 ) );
            rightShape.CollisionGroup = "CharacterSensor";
            Sensor rightSensor = new Sensor( rightShape );
            rightSensor.Name = "right";
            _sensorComponent.Sensors.Add( rightSensor );

            sceneObject.AddComponent( _sensorComponent );
        }

        protected override void OnRemoveFromSceneObject( SceneObject sceneObject )
        {
            sceneObject.RemoveComponent( _sensorComponent );

            _sensorComponent = null;

            base.OnRemoveFromSceneObject( sceneObject );
        }

        protected override void OnAddToScene( SceneGraph scene )
        {
            base.OnAddToScene( scene );

            scene.TimeContext.AddRepeatingTask( _updateTask );
        }

        protected override void OnRemoveFromScene( SceneGraph scene )
        {
            scene.TimeContext.RemoveRepeatingTask( _updateTask );

            base.OnRemoveFromScene( scene );
        }

        public override SceneObjectComponent Clone()
        {
            throw new NotImplementedException();
        }
    }
}
